package com.rocksea.app.fragment;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.rocksea.app.R;
import com.rocksea.app.db.StaticLoad.StaticLoad;
import com.rocksea.app.entry.LoadDataEvent;
import com.rocksea.app.tasks.Temp;
import com.rocksea.app.utils.PerUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;


/**
 *
 * @author 吴志华
 * @date 2017/10/26
 *
 * @FileName:com.rocksea.app.fragment.StaticLoadSampleFrament.java
 */

public class StaticLoadSampleFrament extends CommonBaseInfoFragment {

    public static StaticLoadSampleFrament newInstance(Bundle mBundle)
    {
        StaticLoadSampleFrament frament = new StaticLoadSampleFrament();
        frament.setArguments(mBundle);
        EventBus.getDefault().register(frament);
        return frament;
    }

    @Override
    protected void requestAgain() {
       initData();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void loadDataEvent(LoadDataEvent event)
    {
        initData();
    }

    @Override
    protected void initData() {
        loading();
        StaticLoad mStaticLoad = Temp.getStaticLoad();
        if (mStaticLoad==null)
        {
            loadFailed();
            return;
        }
        ArrayList<StaticLoad.StaticLoadTest> mStaticLoadSamples = mStaticLoad.mStaticLoadTest;
        if (mStaticLoadSamples==null)
        {
            loadFailed();
            return;
        }
        SampleAdapter sampleAdapter = new SampleAdapter(mStaticLoadSamples, getContext());
        pileInfoList.setAdapter(sampleAdapter);
        loadCompleted();
    }
    private class SampleAdapter extends RecyclerView.Adapter<ViewHodler>
    {
        private ArrayList<StaticLoad.StaticLoadTest> staticLoadSamples;
        private Context context;
        private final int mColorGray;
        private SampleAdapter(ArrayList<StaticLoad.StaticLoadTest> staticLoadSamples, Context context)
        {
            this.staticLoadSamples = staticLoadSamples;
            this.context = context;
            mColorGray = Color.rgb(232, 255, 232);
        }

        @Override
        public ViewHodler onCreateViewHolder(ViewGroup viewGroup, int i) {
            View mBootView =  LayoutInflater.from(context).inflate(R.layout.item_details_data,viewGroup,false);
            return new ViewHodler(mBootView);
        }
        @Override
        public void onBindViewHolder(ViewHodler viewHolder, int i)
        {
            StaticLoad.StaticLoadTest sample = staticLoadSamples.get(i);
            viewHolder.tvNumber.setText(String.format("%s",i+1));
            viewHolder.tvNumber.setTextColor(Color.RED);
            viewHolder.tvSampleCount.setText(String.valueOf(sample.sampleCount));
            viewHolder.tvSAverage.setText(String.valueOf(PerUtils.getTwoData(sample.saverage)));
            viewHolder.tvLoading.setText(String.valueOf(PerUtils.getTwoData(sample.loading)));
            viewHolder.tvSampleTime.setText(sample.sampleTime.split(" ")[1]);
            viewHolder.tvSampleValue.setText(getSampleValue(sample.sensorValue,sample.realLoading));
            viewHolder.tvTimeCount.setText(String.valueOf(sample.timeCount));
            viewHolder.itemView.setBackgroundColor((sample.grade%2==0)? Color.WHITE:mColorGray);
            if (sample.isLoad==1)
                viewHolder.ivUploadStatus.setVisibility(View.VISIBLE);
            else
                viewHolder.ivUploadStatus.setVisibility(View.INVISIBLE);
        }
        @Override
        public int getItemCount()
        {
            return staticLoadSamples.size();
        }
    }

    public static class ViewHodler extends RecyclerView.ViewHolder{
        public TextView tvNumber;
        public TextView tvSampleTime;
        public TextView tvLoading;
        public TextView tvSampleCount;
        public TextView tvTimeCount;
        public TextView tvSAverage;
        public TextView tvSampleValue;
        public ImageView ivUploadStatus;
        public ViewHodler(View itemView)
        {
            super(itemView);
            tvNumber = (TextView)itemView.findViewById(R.id.tvNumber);
            tvSampleTime = (TextView)itemView.findViewById(R.id.tvSampleTime);
            tvLoading = (TextView)itemView.findViewById(R.id.tvLoading);
            tvSampleCount = (TextView)itemView.findViewById(R.id.tvSampleCount);
            tvTimeCount = (TextView)itemView.findViewById(R.id.tvTimeCount);
            tvSAverage = (TextView)itemView.findViewById(R.id.tvSAverage);
            tvSampleValue = (TextView)itemView.findViewById(R.id.tvSampleValue);
            ivUploadStatus = (ImageView)itemView.findViewById(R.id.ivUploadStatus);
        }
    }
    private String getSampleValue(float[] sensorValue,double pressValue)
    {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(String.valueOf(PerUtils.getTwoData(pressValue)));
        for (int i = 0; i < sensorValue.length; i++)
        {
            if (sensorValue[i]<200&&sensorValue[i]>-200)
                stringBuilder.append(String.format("    %.2f",PerUtils.getTwoData(sensorValue[i])));
        }
        return stringBuilder.toString();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}

